package com.etc.servlet;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.utils.DruidUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/10  15:46
 **/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.设置编码
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UserDao userDao = new UserDao();
        User user = userDao.login(username);
        if (user == null){
            req.setAttribute("message","用户不存在");
            req.getRequestDispatcher("/errorservlet").forward(req,resp);
        }else {
            if (user.getPassword().equals(password)){
                req.setAttribute("message","登录成sdf功");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }else {
                //创建一个参数
                req.setAttribute("message","密码错误");
                req.getRequestDispatcher("/errorservlet").forward(req,resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}















